/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package logica.jpaControllers;

import Entidades.Pais;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import Entidades.Provincia;
import java.util.ArrayList;
import java.util.Collection;
import Entidades.Proveedor;
import Entidades.Operario;
import Entidades.Transportista;
import logica.jpaControllers.exceptions.IllegalOrphanException;
import logica.jpaControllers.exceptions.NonexistentEntityException;

/**
 *
 * @author Anali
 */
public class PaisJpaController {

    public PaisJpaController() {
        emf = Persistence.createEntityManagerFactory("GestorPesajeJpaPU2");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Pais pais) {
        if (pais.getProvinciaCollection() == null) {
            pais.setProvinciaCollection(new ArrayList<Provincia>());
        }
        if (pais.getProveedorCollection() == null) {
            pais.setProveedorCollection(new ArrayList<Proveedor>());
        }
        if (pais.getOperarioCollection() == null) {
            pais.setOperarioCollection(new ArrayList<Operario>());
        }
        if (pais.getTransportistaCollection() == null) {
            pais.setTransportistaCollection(new ArrayList<Transportista>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Collection<Provincia> attachedProvinciaCollection = new ArrayList<Provincia>();
            for (Provincia provinciaCollectionProvinciaToAttach : pais.getProvinciaCollection()) {
                provinciaCollectionProvinciaToAttach = em.getReference(provinciaCollectionProvinciaToAttach.getClass(), provinciaCollectionProvinciaToAttach.getIdprovincia());
                attachedProvinciaCollection.add(provinciaCollectionProvinciaToAttach);
            }
            pais.setProvinciaCollection(attachedProvinciaCollection);
            Collection<Proveedor> attachedProveedorCollection = new ArrayList<Proveedor>();
            for (Proveedor proveedorCollectionProveedorToAttach : pais.getProveedorCollection()) {
                proveedorCollectionProveedorToAttach = em.getReference(proveedorCollectionProveedorToAttach.getClass(), proveedorCollectionProveedorToAttach.getIdProveedor());
                attachedProveedorCollection.add(proveedorCollectionProveedorToAttach);
            }
            pais.setProveedorCollection(attachedProveedorCollection);
            Collection<Operario> attachedOperarioCollection = new ArrayList<Operario>();
            for (Operario operarioCollectionOperarioToAttach : pais.getOperarioCollection()) {
                operarioCollectionOperarioToAttach = em.getReference(operarioCollectionOperarioToAttach.getClass(), operarioCollectionOperarioToAttach.getIdoperario());
                attachedOperarioCollection.add(operarioCollectionOperarioToAttach);
            }
            pais.setOperarioCollection(attachedOperarioCollection);
            Collection<Transportista> attachedTransportistaCollection = new ArrayList<Transportista>();
            for (Transportista transportistaCollectionTransportistaToAttach : pais.getTransportistaCollection()) {
                transportistaCollectionTransportistaToAttach = em.getReference(transportistaCollectionTransportistaToAttach.getClass(), transportistaCollectionTransportistaToAttach.getIdTransportista());
                attachedTransportistaCollection.add(transportistaCollectionTransportistaToAttach);
            }
            pais.setTransportistaCollection(attachedTransportistaCollection);
            em.persist(pais);
            for (Provincia provinciaCollectionProvincia : pais.getProvinciaCollection()) {
                Pais oldPaisOfProvinciaCollectionProvincia = provinciaCollectionProvincia.getPais();
                provinciaCollectionProvincia.setPais(pais);
                provinciaCollectionProvincia = em.merge(provinciaCollectionProvincia);
                if (oldPaisOfProvinciaCollectionProvincia != null) {
                    oldPaisOfProvinciaCollectionProvincia.getProvinciaCollection().remove(provinciaCollectionProvincia);
                    oldPaisOfProvinciaCollectionProvincia = em.merge(oldPaisOfProvinciaCollectionProvincia);
                }
            }
            for (Proveedor proveedorCollectionProveedor : pais.getProveedorCollection()) {
                Pais oldPaisOfProveedorCollectionProveedor = proveedorCollectionProveedor.getPais();
                proveedorCollectionProveedor.setPais(pais);
                proveedorCollectionProveedor = em.merge(proveedorCollectionProveedor);
                if (oldPaisOfProveedorCollectionProveedor != null) {
                    oldPaisOfProveedorCollectionProveedor.getProveedorCollection().remove(proveedorCollectionProveedor);
                    oldPaisOfProveedorCollectionProveedor = em.merge(oldPaisOfProveedorCollectionProveedor);
                }
            }
            for (Operario operarioCollectionOperario : pais.getOperarioCollection()) {
                Pais oldPaisOfOperarioCollectionOperario = operarioCollectionOperario.getPais();
                operarioCollectionOperario.setPais(pais);
                operarioCollectionOperario = em.merge(operarioCollectionOperario);
                if (oldPaisOfOperarioCollectionOperario != null) {
                    oldPaisOfOperarioCollectionOperario.getOperarioCollection().remove(operarioCollectionOperario);
                    oldPaisOfOperarioCollectionOperario = em.merge(oldPaisOfOperarioCollectionOperario);
                }
            }
            for (Transportista transportistaCollectionTransportista : pais.getTransportistaCollection()) {
                Pais oldPaisOfTransportistaCollectionTransportista = transportistaCollectionTransportista.getPais();
                transportistaCollectionTransportista.setPais(pais);
                transportistaCollectionTransportista = em.merge(transportistaCollectionTransportista);
                if (oldPaisOfTransportistaCollectionTransportista != null) {
                    oldPaisOfTransportistaCollectionTransportista.getTransportistaCollection().remove(transportistaCollectionTransportista);
                    oldPaisOfTransportistaCollectionTransportista = em.merge(oldPaisOfTransportistaCollectionTransportista);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Pais pais) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Pais persistentPais = em.find(Pais.class, pais.getIdpais());
            Collection<Provincia> provinciaCollectionOld = persistentPais.getProvinciaCollection();
            Collection<Provincia> provinciaCollectionNew = pais.getProvinciaCollection();
            Collection<Proveedor> proveedorCollectionOld = persistentPais.getProveedorCollection();
            Collection<Proveedor> proveedorCollectionNew = pais.getProveedorCollection();
            Collection<Operario> operarioCollectionOld = persistentPais.getOperarioCollection();
            Collection<Operario> operarioCollectionNew = pais.getOperarioCollection();
            Collection<Transportista> transportistaCollectionOld = persistentPais.getTransportistaCollection();
            Collection<Transportista> transportistaCollectionNew = pais.getTransportistaCollection();
            List<String> illegalOrphanMessages = null;
            for (Provincia provinciaCollectionOldProvincia : provinciaCollectionOld) {
                if (!provinciaCollectionNew.contains(provinciaCollectionOldProvincia)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Provincia " + provinciaCollectionOldProvincia + " since its pais field is not nullable.");
                }
            }
            for (Proveedor proveedorCollectionOldProveedor : proveedorCollectionOld) {
                if (!proveedorCollectionNew.contains(proveedorCollectionOldProveedor)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Proveedor " + proveedorCollectionOldProveedor + " since its pais field is not nullable.");
                }
            }
            for (Operario operarioCollectionOldOperario : operarioCollectionOld) {
                if (!operarioCollectionNew.contains(operarioCollectionOldOperario)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Operario " + operarioCollectionOldOperario + " since its pais field is not nullable.");
                }
            }
            for (Transportista transportistaCollectionOldTransportista : transportistaCollectionOld) {
                if (!transportistaCollectionNew.contains(transportistaCollectionOldTransportista)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Transportista " + transportistaCollectionOldTransportista + " since its pais field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            Collection<Provincia> attachedProvinciaCollectionNew = new ArrayList<Provincia>();
            for (Provincia provinciaCollectionNewProvinciaToAttach : provinciaCollectionNew) {
                provinciaCollectionNewProvinciaToAttach = em.getReference(provinciaCollectionNewProvinciaToAttach.getClass(), provinciaCollectionNewProvinciaToAttach.getIdprovincia());
                attachedProvinciaCollectionNew.add(provinciaCollectionNewProvinciaToAttach);
            }
            provinciaCollectionNew = attachedProvinciaCollectionNew;
            pais.setProvinciaCollection(provinciaCollectionNew);
            Collection<Proveedor> attachedProveedorCollectionNew = new ArrayList<Proveedor>();
            for (Proveedor proveedorCollectionNewProveedorToAttach : proveedorCollectionNew) {
                proveedorCollectionNewProveedorToAttach = em.getReference(proveedorCollectionNewProveedorToAttach.getClass(), proveedorCollectionNewProveedorToAttach.getIdProveedor());
                attachedProveedorCollectionNew.add(proveedorCollectionNewProveedorToAttach);
            }
            proveedorCollectionNew = attachedProveedorCollectionNew;
            pais.setProveedorCollection(proveedorCollectionNew);
            Collection<Operario> attachedOperarioCollectionNew = new ArrayList<Operario>();
            for (Operario operarioCollectionNewOperarioToAttach : operarioCollectionNew) {
                operarioCollectionNewOperarioToAttach = em.getReference(operarioCollectionNewOperarioToAttach.getClass(), operarioCollectionNewOperarioToAttach.getIdoperario());
                attachedOperarioCollectionNew.add(operarioCollectionNewOperarioToAttach);
            }
            operarioCollectionNew = attachedOperarioCollectionNew;
            pais.setOperarioCollection(operarioCollectionNew);
            Collection<Transportista> attachedTransportistaCollectionNew = new ArrayList<Transportista>();
            for (Transportista transportistaCollectionNewTransportistaToAttach : transportistaCollectionNew) {
                transportistaCollectionNewTransportistaToAttach = em.getReference(transportistaCollectionNewTransportistaToAttach.getClass(), transportistaCollectionNewTransportistaToAttach.getIdTransportista());
                attachedTransportistaCollectionNew.add(transportistaCollectionNewTransportistaToAttach);
            }
            transportistaCollectionNew = attachedTransportistaCollectionNew;
            pais.setTransportistaCollection(transportistaCollectionNew);
            pais = em.merge(pais);
            for (Provincia provinciaCollectionNewProvincia : provinciaCollectionNew) {
                if (!provinciaCollectionOld.contains(provinciaCollectionNewProvincia)) {
                    Pais oldPaisOfProvinciaCollectionNewProvincia = provinciaCollectionNewProvincia.getPais();
                    provinciaCollectionNewProvincia.setPais(pais);
                    provinciaCollectionNewProvincia = em.merge(provinciaCollectionNewProvincia);
                    if (oldPaisOfProvinciaCollectionNewProvincia != null && !oldPaisOfProvinciaCollectionNewProvincia.equals(pais)) {
                        oldPaisOfProvinciaCollectionNewProvincia.getProvinciaCollection().remove(provinciaCollectionNewProvincia);
                        oldPaisOfProvinciaCollectionNewProvincia = em.merge(oldPaisOfProvinciaCollectionNewProvincia);
                    }
                }
            }
            for (Proveedor proveedorCollectionNewProveedor : proveedorCollectionNew) {
                if (!proveedorCollectionOld.contains(proveedorCollectionNewProveedor)) {
                    Pais oldPaisOfProveedorCollectionNewProveedor = proveedorCollectionNewProveedor.getPais();
                    proveedorCollectionNewProveedor.setPais(pais);
                    proveedorCollectionNewProveedor = em.merge(proveedorCollectionNewProveedor);
                    if (oldPaisOfProveedorCollectionNewProveedor != null && !oldPaisOfProveedorCollectionNewProveedor.equals(pais)) {
                        oldPaisOfProveedorCollectionNewProveedor.getProveedorCollection().remove(proveedorCollectionNewProveedor);
                        oldPaisOfProveedorCollectionNewProveedor = em.merge(oldPaisOfProveedorCollectionNewProveedor);
                    }
                }
            }
            for (Operario operarioCollectionNewOperario : operarioCollectionNew) {
                if (!operarioCollectionOld.contains(operarioCollectionNewOperario)) {
                    Pais oldPaisOfOperarioCollectionNewOperario = operarioCollectionNewOperario.getPais();
                    operarioCollectionNewOperario.setPais(pais);
                    operarioCollectionNewOperario = em.merge(operarioCollectionNewOperario);
                    if (oldPaisOfOperarioCollectionNewOperario != null && !oldPaisOfOperarioCollectionNewOperario.equals(pais)) {
                        oldPaisOfOperarioCollectionNewOperario.getOperarioCollection().remove(operarioCollectionNewOperario);
                        oldPaisOfOperarioCollectionNewOperario = em.merge(oldPaisOfOperarioCollectionNewOperario);
                    }
                }
            }
            for (Transportista transportistaCollectionNewTransportista : transportistaCollectionNew) {
                if (!transportistaCollectionOld.contains(transportistaCollectionNewTransportista)) {
                    Pais oldPaisOfTransportistaCollectionNewTransportista = transportistaCollectionNewTransportista.getPais();
                    transportistaCollectionNewTransportista.setPais(pais);
                    transportistaCollectionNewTransportista = em.merge(transportistaCollectionNewTransportista);
                    if (oldPaisOfTransportistaCollectionNewTransportista != null && !oldPaisOfTransportistaCollectionNewTransportista.equals(pais)) {
                        oldPaisOfTransportistaCollectionNewTransportista.getTransportistaCollection().remove(transportistaCollectionNewTransportista);
                        oldPaisOfTransportistaCollectionNewTransportista = em.merge(oldPaisOfTransportistaCollectionNewTransportista);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = pais.getIdpais();
                if (findPais(id) == null) {
                    throw new NonexistentEntityException("The pais with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Pais pais;
            try {
                pais = em.getReference(Pais.class, id);
                pais.getIdpais();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The pais with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<Provincia> provinciaCollectionOrphanCheck = pais.getProvinciaCollection();
            for (Provincia provinciaCollectionOrphanCheckProvincia : provinciaCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Pais (" + pais + ") cannot be destroyed since the Provincia " + provinciaCollectionOrphanCheckProvincia + " in its provinciaCollection field has a non-nullable pais field.");
            }
            Collection<Proveedor> proveedorCollectionOrphanCheck = pais.getProveedorCollection();
            for (Proveedor proveedorCollectionOrphanCheckProveedor : proveedorCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Pais (" + pais + ") cannot be destroyed since the Proveedor " + proveedorCollectionOrphanCheckProveedor + " in its proveedorCollection field has a non-nullable pais field.");
            }
            Collection<Operario> operarioCollectionOrphanCheck = pais.getOperarioCollection();
            for (Operario operarioCollectionOrphanCheckOperario : operarioCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Pais (" + pais + ") cannot be destroyed since the Operario " + operarioCollectionOrphanCheckOperario + " in its operarioCollection field has a non-nullable pais field.");
            }
            Collection<Transportista> transportistaCollectionOrphanCheck = pais.getTransportistaCollection();
            for (Transportista transportistaCollectionOrphanCheckTransportista : transportistaCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Pais (" + pais + ") cannot be destroyed since the Transportista " + transportistaCollectionOrphanCheckTransportista + " in its transportistaCollection field has a non-nullable pais field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(pais);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Pais> findPaisEntities() {
        return findPaisEntities(true, -1, -1);
    }

    public List<Pais> findPaisEntities(int maxResults, int firstResult) {
        return findPaisEntities(false, maxResults, firstResult);
    }

    private List<Pais> findPaisEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select object(o) from Pais as o");
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Pais findPais(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Pais.class, id);
        } finally {
            em.close();
        }
    }

    public int getPaisCount() {
        EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select count(o) from Pais as o");
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    public Pais findPais_x_nombre(String nom) {
          EntityManager em = getEntityManager();
        try {
            Query q = em.createQuery("select u from Pais u where u.nombrepais = " + "'" + nom +"'");
            return (Pais) q.getSingleResult();
        } finally {
            em.close();
        }
    }

}
